package net.easyjava.third.rongcloud.rong;


import com.alibaba.fastjson.JSONObject;
import net.easyjava.third.rongcloud.rong.models.*;
import net.easyjava.third.rongcloud.rong.util.HttpUtil;
import net.easyjava.tools.lang.StringTools;

import java.io.IOException;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URLEncoder;
import java.util.List;

public class ApiHttpClient implements Serializable {
 
	/**
	 * 
	 */
	private static final long serialVersionUID = -2891450519612289086L;

	private static final String RONGCLOUDURI = "https://api.cn.ronghub.com";//"https://api.cn.rong.io";
	
	private static final String UTF8 = "UTF-8";

	/**
	 * 获取token
	 * name:getToken
	 * des:
	 * author:wxnacy
	 * date:2015-6-11下午6:30:34
	 * @param appKey
	 * @param appSecret
	 * @param userId
	 * @param userName
	 * @param portraitUri
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult getToken(String appKey, String appSecret,
			String userId, String userName, String portraitUri) throws IOException {

		HttpURLConnection conn = HttpUtil
				.CreatePostHttpConnection(appKey, appSecret, RONGCLOUDURI
						+ "/user/getToken." + FormatType.json.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("userId=").append(URLEncoder.encode(userId, UTF8));
		sb.append("&name=").append(URLEncoder.encode(userName, UTF8));
		sb.append("&portraitUri=").append(URLEncoder.encode(portraitUri, UTF8));
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 检查用户在线状态
	 * name:checkOnline
	 * des:
	 * author:wxnacy
	 * date:2015-6-11下午6:30:26
	 * @param appKey
	 * @param appSecret
	 * @param userId
	 * @param format
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult checkOnline(String appKey, String appSecret,
			String userId, FormatType format) throws IOException {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				RONGCLOUDURI + "/user/checkOnline." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("userId=").append(URLEncoder.encode(userId, UTF8));
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 刷新用户信息
	 * name:refreshUser
	 * des:
	 * author:wxnacy
	 * date:2015-6-11下午6:30:45
	 * @param appKey
	 * @param appSecret
	 * @param userId
	 * @param userName
	 * @param portraitUri
	 * @param format
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult refreshUser(String appKey, String appSecret,
			String userId, String userName, String portraitUri,
			FormatType format) throws IOException {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret, RONGCLOUDURI + "/user/refresh." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("userId=").append(URLEncoder.encode(userId, UTF8));
		sb.append("&name=").append(URLEncoder.encode(userName, UTF8));
		sb.append("&portraitUri=").append(URLEncoder.encode(portraitUri, UTF8));

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	
	/**
	 * // 封禁用户
	 * name:blockUser
	 * des:
	 * author:wxnacy
	 * date:2015-6-11下午6:31:11
	 * @param appKey
	 * @param appSecret
	 * @param userId
	 * @param minute
	 * @param format
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult blockUser(String appKey, String appSecret,
			String userId, int minute, FormatType format) throws IOException {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret, RONGCLOUDURI + "/user/block." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("userId=").append(URLEncoder.encode(userId, UTF8));
		sb.append("&minute=").append(
				URLEncoder.encode(String.valueOf(minute), UTF8));

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	
	/**
	 * // 解禁用户
	 * name:unblockUser
	 * des:
	 * author:wxnacy
	 * date:2015-6-11下午6:31:19
	 * @param appKey
	 * @param appSecret
	 * @param userId
	 * @param format
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult unblockUser(String appKey, String appSecret,
			String userId, FormatType format) throws IOException {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret, RONGCLOUDURI + "/user/unblock." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("userId=").append(URLEncoder.encode(userId, UTF8));

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 获取被封禁用户
	 * name:queryBlockUsers
	 * des:
	 * author:wxnacy
	 * date:2015-6-11下午6:31:26
	 * @param appKey
	 * @param appSecret
	 * @param format
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult queryBlockUsers(String appKey,
			String appSecret, FormatType format) throws IOException {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				RONGCLOUDURI + "/user/block/query." + format.toString());

		return HttpUtil.returnResult(conn);
	}

	
	/**
	 * // 添加用户到黑名单
	 * name:blackUser
	 * des:
	 * author:wxnacy
	 * date:2015-6-11下午6:31:40
	 * @param appKey
	 * @param appSecret
	 * @param userId
	 * @param blackUserIds
	 * @param format
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult blackUser(String appKey, String appSecret,
			String userId, List<String> blackUserIds, FormatType format)
			throws IOException {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				RONGCLOUDURI + "/user/blacklist/add." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("userId=").append(URLEncoder.encode(userId, UTF8));
		if (blackUserIds != null) {
			for (String blackId : blackUserIds) {
				sb.append("&blackUserId=").append(
						URLEncoder.encode(blackId, UTF8));
			}
		}

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	
	/**
	 * // 从黑名单移除用户
	 * name:unblackUser
	 * des:
	 * author:wxnacy
	 * date:2015-6-11下午6:31:48
	 * @param appKey
	 * @param appSecret
	 * @param userId
	 * @param blackUserIds
	 * @param format
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult unblackUser(String appKey, String appSecret,
			String userId, List<String> blackUserIds, FormatType format)
			throws IOException {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				RONGCLOUDURI + "/user/blacklist/remove." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("userId=").append(URLEncoder.encode(userId, UTF8));
		if (blackUserIds != null) {
			for (String blackId : blackUserIds) {
				sb.append("&blackUserId=").append(
						URLEncoder.encode(blackId, UTF8));
			}
		}

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	
	/**
	 * // 获取黑名单用户
	 * name:QueryblackUser
	 * des:
	 * author:wxnacy
	 * date:2015-6-11下午6:31:55
	 * @param appKey
	 * @param appSecret
	 * @param userId
	 * @param format
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult QueryblackUser(String appKey, String appSecret,
			String userId, FormatType format) throws IOException {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				RONGCLOUDURI + "/user/blacklist/query." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("userId=").append(URLEncoder.encode(userId, UTF8));

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	
	/**
	 * // 创建群
	 * name:createGroup
	 * des:
	 * author:wxnacy
	 * date:2015-6-11下午6:32:03
	 * @param appKey
	 * @param appSecret
	 * @param userIds
	 * @param groupId
	 * @param groupName
	 * @param format
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult createGroup(String appKey, String appSecret,
			List<String> userIds, String groupId, String groupName,
			FormatType format) throws IOException {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret, RONGCLOUDURI + "/group/create." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("groupId=").append(URLEncoder.encode(groupId, UTF8));
		sb.append("&groupName=").append(URLEncoder.encode(groupName, UTF8));
		if (userIds != null) {
			for (String id : userIds) {
				sb.append("&userId=").append(URLEncoder.encode(id, UTF8));
			}
		}
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	
	/**
	 * // 加入群
	 * name:joinGroup
	 * des:
	 * author:wxnacy
	 * date:2015-6-11下午6:32:10
	 * @param appKey
	 * @param appSecret
	 * @param userId
	 * @param groupId
	 * @param groupName
	 * @param format
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult joinGroup(String appKey, String appSecret,
			String userId, String groupId, String groupName, FormatType format)
			throws IOException {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret, RONGCLOUDURI + "/group/join." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("userId=").append(URLEncoder.encode(userId, UTF8));
		sb.append("&groupId=").append(URLEncoder.encode(groupId, UTF8));
		sb.append("&groupName=").append(URLEncoder.encode(groupName, UTF8));
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	
	/**
	 * // 批量加入群
	 * name:joinGroupBatch
	 * des:
	 * author:wxnacy
	 * date:2015-6-11下午6:32:16
	 * @param appKey
	 * @param appSecret
	 * @param userIds
	 * @param groupId
	 * @param groupName
	 * @param format
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult joinGroupBatch(String appKey, String appSecret,
			List<String> userIds, String groupId, String groupName,
			FormatType format) throws IOException {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret, RONGCLOUDURI + "/group/join." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("groupId=").append(URLEncoder.encode(groupId, UTF8));
		sb.append("&groupName=").append(URLEncoder.encode(groupName, UTF8));
		if (userIds != null) {
			for (String id : userIds) {
				sb.append("&userId=").append(URLEncoder.encode(id, UTF8));
			}
		}
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	
	/**
	 * // 退出群
	 * name:quitGroup
	 * des:
	 * author:wxnacy
	 * date:2015-6-11下午6:32:26
	 * @param appKey
	 * @param appSecret
	 * @param userId
	 * @param groupId
	 * @param format
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult quitGroup(String appKey, String appSecret,
			String userId, String groupId, FormatType format) throws IOException {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret, RONGCLOUDURI + "/group/quit." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("userId=").append(URLEncoder.encode(userId, UTF8));
		sb.append("&groupId=").append(URLEncoder.encode(groupId, UTF8));
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	
	/**
	 * // 批量退出群
	 * name:quitGroupBatch
	 * des:
	 * author:wxnacy
	 * date:2015-6-11下午6:32:32
	 * @param appKey
	 * @param appSecret
	 * @param userIds
	 * @param groupId
	 * @param format
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult quitGroupBatch(String appKey, String appSecret,
			List<String> userIds, String groupId, FormatType format)
			throws IOException {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret, RONGCLOUDURI + "/group/quit." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("groupId=").append(URLEncoder.encode(groupId, UTF8));
		if (userIds != null) {
			for (String id : userIds) {
				sb.append("&userId=").append(URLEncoder.encode(id, UTF8));
			}
		}

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	
	/**
	 * // 解散群
	 * name:dismissGroup
	 * des:
	 * author:wxnacy
	 * date:2015-6-11下午6:32:39
	 * @param appKey
	 * @param appSecret
	 * @param userId
	 * @param groupId
	 * @param format
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult dismissGroup(String appKey, String appSecret,
			String userId, String groupId, FormatType format) throws IOException {

		HttpURLConnection conn = HttpUtil
				.CreatePostHttpConnection(appKey, appSecret, RONGCLOUDURI
						+ "/group/dismiss." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("userId=").append(URLEncoder.encode(userId, UTF8));
		sb.append("&groupId=").append(URLEncoder.encode(groupId, UTF8));
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	// 同步用户群信息
	public static SdkHttpResult syncGroup(String appKey, String appSecret,
			String userId, List<GroupInfo> groups, FormatType format)
			throws IOException {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret, RONGCLOUDURI + "/group/sync." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("userId=").append(URLEncoder.encode(userId, UTF8));
		if (groups != null) {
			for (GroupInfo info : groups) {
				if (info != null) {
					sb.append(
							String.format("&group[%s]=",
									URLEncoder.encode(info.getId(), UTF8)))
							.append(URLEncoder.encode(info.getName(), UTF8));
				}
			}
		}
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	// 刷新群信息
	public static SdkHttpResult refreshGroupInfo(String appKey,
			String appSecret, String groupId, String groupName,
			FormatType format) throws IOException {

		HttpURLConnection conn = HttpUtil
				.CreatePostHttpConnection(appKey, appSecret, RONGCLOUDURI
						+ "/group/refresh." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("groupId=").append(URLEncoder.encode(groupId, UTF8));
		sb.append("&groupName=").append(URLEncoder.encode(groupName, UTF8));

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	// 刷新群信息
	public static SdkHttpResult refreshGroupInfo(String appKey,
			String appSecret, GroupInfo group, FormatType format)
			throws IOException {

		HttpURLConnection conn = HttpUtil
				.CreatePostHttpConnection(appKey, appSecret, RONGCLOUDURI
						+ "/group/refresh." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("groupId=").append(URLEncoder.encode(group.getId(), UTF8));
		sb.append("&groupName=").append(
				URLEncoder.encode(group.getName(), UTF8));

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	
	/**
	 * // 发送消息
	 * name:publishMessage
	 * des:
	 * author:wxnacy
	 * date:2015-6-11下午6:32:46
	 * @param appKey
	 * @param appSecret
	 * @param fromUserId
	 * @param toUserIds
	 * @param msg
	 * @param format
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult publishMessage(String appKey, String appSecret,
			String fromUserId, List<String> toUserIds, Message msg,
			FormatType format) throws IOException {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				RONGCLOUDURI + "/message/private/publish." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("fromUserId=").append(URLEncoder.encode(fromUserId, UTF8));
		if (toUserIds != null) {
			for (int i = 0; i < toUserIds.size(); i++) {
				sb.append("&toUserId=").append(
						URLEncoder.encode(toUserIds.get(i), UTF8));
			}
		}
		sb.append("&objectName=")
				.append(URLEncoder.encode(msg.getType(), UTF8));
		sb.append("&content=").append(URLEncoder.encode(msg.toString(), UTF8));

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	
	/**
	 * // 发送消息
	 * name:publishMessage
	 * des:
	 * author:wxnacy
	 * date:2015-6-11下午6:32:53
	 * @param appKey
	 * @param appSecret
	 * @param fromUserId
	 * @param toUserIds
	 * @param msg
	 * @param pushContent
	 * @param pushData
	 * @param format
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult publishMessage(String appKey, String appSecret,
			String fromUserId, List<String> toUserIds, Message msg,
			String pushContent, String pushData, FormatType format)
			throws IOException {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				RONGCLOUDURI + "/message/publish." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("fromUserId=").append(URLEncoder.encode(fromUserId, UTF8));
		if (toUserIds != null) {
			for (int i = 0; i < toUserIds.size(); i++) {
				sb.append("&toUserId=").append(
						URLEncoder.encode(toUserIds.get(i), UTF8));
			}
		}
		sb.append("&objectName=")
				.append(URLEncoder.encode(msg.getType(), UTF8));
		sb.append("&content=").append(URLEncoder.encode(msg.toString(), UTF8));

		if (pushContent != null) {
			sb.append("&pushContent=").append(
					URLEncoder.encode(pushContent, UTF8));
		}

		if (pushData != null) {
			sb.append("&pushData=").append(URLEncoder.encode(pushData, UTF8));
		}

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	
	/**
	 * // 发送系统消息
	 * name:publishSystemMessage
	 * des:
	 * author:wxnacy
	 * date:2015-6-11下午6:33:00
	 * @param appKey
	 * @param appSecret
	 * @param fromUserId
	 * @param toUserIds
	 * @param msg
	 * @param pushContent
	 * @param pushData
	 * @param format
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult publishSystemMessage(String appKey,
			String appSecret, String fromUserId, List<String> toUserIds,
			Message msg, String pushContent, String pushData, FormatType format)
			throws IOException {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				RONGCLOUDURI + "/message/system/publish." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("fromUserId=").append(URLEncoder.encode(fromUserId, UTF8));
		if (toUserIds != null) {
			for (int i = 0; i < toUserIds.size(); i++) {
				sb.append("&toUserId=").append(
						URLEncoder.encode(toUserIds.get(i), UTF8));
			}
		}
		sb.append("&objectName=")
				.append(URLEncoder.encode(msg.getType(), UTF8));
		sb.append("&content=").append(URLEncoder.encode(msg.toString(), UTF8));

		if (pushContent != null) {
			sb.append("&pushContent=").append(
					URLEncoder.encode(pushContent, UTF8));
		}

		if (pushData != null) {
			sb.append("&pushData=").append(URLEncoder.encode(pushData, UTF8));
		}
//		System.out.println("sb.toString() = " + sb.toString());

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	
	/**
	 * // 发送群消息
	 * name:publishGroupMessage
	 * des:
	 * author:wxnacy
	 * date:2015-6-11下午6:33:10
	 * @param appKey
	 * @param appSecret
	 * @param fromUserId
	 * @param toGroupIds
	 * @param msg
	 * @param pushContent
	 * @param pushData
	 * @param format
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult publishGroupMessage(String appKey,
			String appSecret, String fromUserId, List<String> toGroupIds,
			Message msg, String pushContent, String pushData, FormatType format)
			throws IOException {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				RONGCLOUDURI + "/message/group/publish." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("fromUserId=").append(URLEncoder.encode(fromUserId, UTF8));
		if (toGroupIds != null) {
			for (int i = 0; i < toGroupIds.size(); i++) {
				sb.append("&toGroupId=").append(
						URLEncoder.encode(toGroupIds.get(i), UTF8));
			}
		}
		sb.append("&objectName=")
				.append(URLEncoder.encode(msg.getType(), UTF8));
		sb.append("&content=").append(URLEncoder.encode(msg.toString(), UTF8));

		if (pushContent != null) {
			sb.append("&pushContent=").append(
					URLEncoder.encode(pushContent, UTF8));
		}

		if (pushData != null) {
			sb.append("&pushData=").append(URLEncoder.encode(pushData, UTF8));
		}

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	
	/**
	 * // 发送聊天室消息
	 * name:publishChatroomMessage
	 * des:
	 * author:wxnacy
	 * date:2015-6-11下午6:33:23
	 * @param appKey
	 * @param appSecret
	 * @param fromUserId
	 * @param toChatroomIds
	 * @param msg
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult publishChatroomMessage(String appKey,
			String appSecret, String fromUserId, List<String> toChatroomIds,
			Message msg) throws IOException {

		HttpURLConnection conn = HttpUtil
				.CreatePostHttpConnection(appKey, appSecret, RONGCLOUDURI
						+ "/message/chatroom/publish." + FormatType.json.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("fromUserId=").append(URLEncoder.encode(fromUserId, UTF8));
		if (toChatroomIds != null) {
			for (int i = 0; i < toChatroomIds.size(); i++) {
				sb.append("&toChatroomId=").append(
						URLEncoder.encode(toChatroomIds.get(i), UTF8));
			}
		}
		sb.append("&objectName=")
				.append(URLEncoder.encode(msg.getType(), UTF8));
		sb.append("&content=").append(URLEncoder.encode(msg.toString(), UTF8));
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 发送广播消息
	 * @param appKey
	 * @param appSecret
	 * @param fromUserId
	 * @param msg
	 * @param os
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult publishBroadcastMessage(String appKey,
			String appSecret, String fromUserId,
			Message msg,String os) throws IOException {

		HttpURLConnection conn = HttpUtil
				.CreatePostHttpConnection(appKey, appSecret, RONGCLOUDURI
						+ "/message/broadcast." + FormatType.json.toString());
		StringBuilder sb = new StringBuilder();
		sb.append("fromUserId=").append(URLEncoder.encode(fromUserId, UTF8));
		sb.append("&objectName=")
				.append(URLEncoder.encode(msg.getType(), UTF8));
		sb.append("&content=").append(URLEncoder.encode(msg.toString(), UTF8));
		if(!StringTools.isEmpty(os)){
			sb.append("&os=").append(URLEncoder.encode(os,UTF8));
		}

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}


	/**
	 * 发送广播推送
	 * @param appKey
	 * @param appSecret
	 * @param json
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult publishBroadcastPushMessage(String appKey,
			String appSecret, String json) throws IOException {

		HttpURLConnection conn = HttpUtil.CreateJsonPostHttpConnection(appKey,
				appSecret, RONGCLOUDURI + "/push." + FormatType.json.toString());


		HttpUtil.setBodyParameter(json, conn);

		return HttpUtil.returnResult(conn);
	}


	/**
	 * 设置标签 推送使用
	 * @param appKey
	 * @param appSecret
	 * @param userId
	 * @param tags
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult setTags(String appKey,
			String appSecret, String userId,String... tags) throws IOException {

		HttpURLConnection conn = HttpUtil.CreateJsonPostHttpConnection(appKey,
				appSecret, RONGCLOUDURI + "/user/tag/set." + FormatType.json.toString());
		JSONObject params = new JSONObject();
		params.put("userId",userId);
		params.put("tags",tags);
		HttpUtil.setBodyParameter(params.toString(), conn);

		return HttpUtil.returnResult(conn);
	}


	/**
	 * 查询聊天内用户
	 * @param appKey
	 * @param appSecret
	 * @param chatroomId
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult queryChatroomUsers(String appKey,
			String appSecret, String chatroomId) throws IOException {

		HttpURLConnection conn = HttpUtil
				.CreatePostHttpConnection(appKey, appSecret, RONGCLOUDURI
						+ "/chatroom/user/query." + FormatType.json.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("chatroomId=").append(URLEncoder.encode(chatroomId, UTF8));
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}



	
	/**
	 * // 创建聊天室
	 * name:createChatroom
	 * des:
	 * author:wxnacy
	 * date:2015-6-11下午6:33:36
	 * @param appKey
	 * @param appSecret
	 * @param chatrooms
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult createChatroom(String appKey, String appSecret,
			List<ChatroomInfo> chatrooms) throws IOException {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				RONGCLOUDURI + "/chatroom/create." + FormatType.json.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("1=1");
		if (chatrooms != null) {
			for (ChatroomInfo info : chatrooms) {
				if (info != null) {
					sb.append(
							String.format("&chatroom[%s]=",
									URLEncoder.encode(info.getId(), UTF8)))
							.append(URLEncoder.encode(info.getName(), UTF8));
				}
			}
		}
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 加入聊天室
	 * @param appKey
	 * @param appSecret
	 * @param userIds
	 * @param chatroomId
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult joinChatroom(String appKey, String appSecret,
			List<String> userIds,String chatroomId ) throws IOException {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				RONGCLOUDURI + "/chatroom/join." + FormatType.json.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("chatroomId=").append(chatroomId);
		if (userIds != null) {
			for (String info : userIds) {
				if (info != null) {
					sb.append("&userId=").append(info);
				}
			}
		}
		HttpUtil.setBodyParameter(sb, conn);


		return HttpUtil.returnResult(conn);
	}

	
	/**
	 * // 销毁聊天室
	 * name:destroyChatroom
	 * des:
	 * author:wxnacy
	 * date:2015-6-11下午6:33:43
	 * @param appKey
	 * @param appSecret
	 * @param chatroomIds
	 * @param format
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult destroyChatroom(String appKey,
			String appSecret, List<String> chatroomIds, FormatType format)
			throws IOException {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				RONGCLOUDURI + "/chatroom/destroy." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("1=1");
		if (chatroomIds != null) {
			for (String id : chatroomIds) {
				sb.append("&chatroomId=").append(URLEncoder.encode(id, UTF8));
			}
		}

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	
	/**
	 * // 查询聊天室信息
	 * name:queryChatroom
	 * des:
	 * author:wxnacy
	 * date:2015-6-11下午6:33:51
	 * @param appKey
	 * @param appSecret
	 * @param chatroomIds
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult queryChatroom(String appKey, String appSecret,
			List<String> chatroomIds) throws IOException {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				RONGCLOUDURI + "/chatroom/query." + FormatType.json.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("1=1");
		if (chatroomIds != null) {
			for (String id : chatroomIds) {
				sb.append("&chatroomId=").append(URLEncoder.encode(id, UTF8));
			}
		}

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	
	/**
	 * // 获取消息历史记录下载地址
	 * name:getMessageHistoryUrl
	 * des:
	 * author:wxnacy
	 * date:2015-6-11下午6:33:58
	 * @param appKey
	 * @param appSecret
	 * @param date
	 * @param format
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult getMessageHistoryUrl(String appKey,
			String appSecret, String date, FormatType format) throws IOException {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				RONGCLOUDURI + "/message/history." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("date=").append(URLEncoder.encode(date, UTF8));
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	
	/**
	 * // 删除消息历史记录
	 * name:deleteMessageHistory
	 * des:
	 * author:wxnacy
	 * date:2015-6-11下午6:34:06
	 * @param appKey
	 * @param appSecret
	 * @param date
	 * @param format
	 * @return
	 * @throws IOException
	 */
	public static SdkHttpResult deleteMessageHistory(String appKey,
			String appSecret, String date, FormatType format) throws IOException {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				RONGCLOUDURI + "/message/history/delete." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("date=").append(URLEncoder.encode(date, UTF8));
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

}
